package com.orphan.audit.controller;


import com.orphan.audit.po.SupplierReturnCommodity;
import com.orphan.audit.service.ISupplierReturnAuditService;
import com.orphan.audit.service.ISupplierReturnCommodityService;
import com.orphan.util.dto.SettlementSupplierGoodsDto;
import com.orphan.util.dto.SupplierReturnDto;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yichen
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/supplierreturn-audit")
public class SupplierReturnAuditController {
    @Autowired
    ISupplierReturnAuditService supplierReturnAuditService;
    @Autowired
    ISupplierReturnCommodityService supplierReturnCommodityService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("addition")
    public String addition(){
        List<SupplierReturnCommodity> goodsList = supplierReturnCommodityService.findAll();
        System.out.println(supplierReturnCommodityService.findAll());
        List<SettlementSupplierGoodsDto> settlementList = new ArrayList<>();
        SupplierReturnDto dto = new SupplierReturnDto();
        SettlementSupplierGoodsDto goodsDto = new SettlementSupplierGoodsDto();
        for (int i = 0; i < goodsList.size(); i++) {
            dto.setTOrderId(goodsList.get(i).getSupplierReturnAudit().getTOrderId());
            dto.setTSupplierName(goodsList.get(i).getSupplierReturnAudit().getTSupplierName());
            dto.setTOrderTime(goodsList.get(i).getSupplierReturnAudit().getTOrdertime());
            dto.setTTotal(goodsList.get(i).getSupplierReturnAudit().getTTotal());
            dto.setTState(goodsList.get(i).getSupplierReturnAudit().getTState());

            //供应商品集合
            goodsDto.setGoodsId(goodsList.get(i).getTId());
            goodsDto.setGoodsName(goodsList.get(i).getGoodsName());
            goodsDto.setGoodsNum(goodsList.get(i).getGoodsNum());
            goodsDto.setSize(goodsList.get(i).getSize());
            goodsDto.setType(goodsList.get(i).getType());
            goodsDto.setPrice(goodsList.get(i).getPrice());
            goodsDto.setTFkId(goodsList.get(i).getTFkId());
            settlementList.add(goodsDto);
            //商品集合放入dto中
            dto.setSupplierGoods(settlementList);
            rabbitTemplate.convertAndSend("directExchangeaudit","audit",dto);
        }
        return "ok";
    }
//    @RequestMapping("test")
//    @RabbitListener(queues = "queueaudit")
//   public void get(SupplierReturnDto dto){
//       System.out.println("dto = " + dto);
//   }

}
